<?php
/*  Copyright 2008  Jacob Brunson  (email : wp-jacob.gpl.license@jacobbrunson.com )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


function display_select_countries($name, $default)
{
    $country_codes = array('US'=>'USA', 'CA'=>'Canada', 'MX'=>'Mexico', 'GR'=>'Greece', 'FR'=>'France', 'GB'=>'United Kingdom', 'AU'=>'Australia' );
    echo "<select name=\"$name\">";
    echo '<option value="none">Select One</option>';
    foreach ($country_codes as $code=>$country_name) {
        $selected = $default==$code ? ' selected="selected" ' : '';
        echo "<option value=\"$code\" $selected>$country_name</option>";
    }
    echo "</select>";
}

function decode_shipping_string($string)
{
    $out = array();
    $entries = explode(';', $string);
    foreach ($entries as $entry) {
        $values = explode(',', $entry);
        $row = array();
        $row['country'] = $values[0];
        $row['base'] = (float)$values[1];
        if (count($values) == 3) {
            $row['incr'] = (float)$values[2];
        } else {
            $row['incr'] = 0;
        }
        $out[] = $row;
    }
    return $out;
}
?>

<style type="text/css">
.templaterow {
    display: none;
}

.ratetable {
    margin-top: 10px;
}

table.ratetable {
    border: 3px solid #EAF3FA;
    border-collapse: collapse;
    background-color: white;
}
table.ratetable th {
    border: 2px solid #EAF3FA;
    padding: 4px;
    background-color: #EAF3FA;
}
table.ratetable td {
    border: 2px solid #EAF3FA;
    padding: 4px;
    background-color: white;
}

#flat-rate-table tr.templaterow {
    background-color: blue;
}
</style>
<div class="wrap">
<h2>Shopping Cart Options</h2>

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<h3>General Options</h3>
<table class="form-table">

<tr valign="top">
<th scope="row">Currency</th>
<?php $s_currency = get_option('shopcart_currency'); ?>
<td><select name="shopcart_currency">
    <option value="USD" selected="selected" <?php if ($s_currency == 'USD') echo 'selected="selected"'; ?>>USD</option>
</select></td>
</tr>
 
<tr valign="top">
<th scope="row">Weight Units</th>
<td>
    <select name="shopcart_weight_units">
        <?php $s_weight_unit = get_option('shopcart_weight_units'); ?>
        <option value="lbs" <?php if ($s_weight_unit == 'lbs') echo 'selected="selected"'; ?>>Pounds (lbs)</option>
        <option value="oz" <?php if ($s_weight_unit == 'oz') echo 'selected="selected"'; ?>>Ounces (oz)</option>
    </select>
</td>
</tr>

</table>

<h3>Shopping Cart Options</h3>
<table class="form-table">

<tr valign="top">
<th scope="row">Sandbox Mode</th>
<td>
    <select name="shopcart_sandbox_mode" onchange="changeSandboxMode(this);">
        <?php $s_sandmode = get_option('shopcart_sandbox_mode'); ?>
        <option value="true" <?php if ($s_sandmode == 'true') echo 'selected="selected"'; ?>>On</option>
        <option value="false" <?php if ($s_sandmode == 'false') echo 'selected="selected"'; ?>>Off</option>
    </select>
</td>
</tr>
 
<tr valign="top">
<th scope="row">Hide cart when empty</th>
<td>
    <select name="shopcart_hide_empty_cart">
        <?php $s_h_e_c = get_option('shopcart_hide_empty_cart'); ?>
        <option value="true" <?php if ($s_h_e_c == 'true') echo 'selected="selected"'; ?>>Yes</option>
        <option value="false" <?php if ($s_h_e_c == 'false') echo 'selected="selected"'; ?>>No</option>
    </select>
</td>
</tr>

</table>

<h3>Shopping Cart Appearence</h3>
<em>Leave these options to use default.  Use color formats like <strong>#878787</strong> or <strong>rbg(100,200,220)</strong>.</em>
<table class="form-table">

<tr valign="top">
<th scope="row">Header Background Color</th>
<td>
    <input name="shopcart_first_color" value="<?php echo get_option('shopcart_first_color');?>" size="16"/>
</td>
</tr>
 
<tr valign="top">
<th scope="row">Border Color</th>
<td>
    <input name="shopcart_second_color" value="<?php echo get_option('shopcart_second_color');?>" size="16"/>
</td>
</tr>

<tr valign="top">
<th scope="row">Header Text Color</th>
<td>
    <input name="shopcart_third_color" value="<?php echo get_option('shopcart_third_color');?>" size="16"/>
</td>
</tr>

<tr valign="top">
<th scope="row">Link Text Color</th>
<td>
    <input name="shopcart_fourth_color" value="<?php echo get_option('shopcart_fourth_color');?>" size="16"/>
</td>
</tr>

</table>

<h3>Google Integration Options</h3>
<table class="form-table">

<tr valign="top" <?php if ($s_sandmode != 'true') echo 'style="display:none;" '; ?> id="sandboxform">
<th scope="row">Sandbox Merchant ID</th>
<td><input type="text" name="shopcart_sandbox_mid" value="<?php echo get_option('shopcart_sandbox_mid'); ?>" /></td>
</tr>

<tr valign="top">
<th scope="row">Google Checkout Merchant ID</th>
<td><input type="text" name="shopcart_mid" value="<?php echo get_option('shopcart_mid'); ?>" /></td>
</tr>
 
<tr valign="top">
<th scope="row">Google Analytics ID (optional)</th>
<td><input type="text" name="shopcart_aid" value="<?php echo get_option('shopcart_aid'); ?>" /></td>
</tr>

<<tr valign="top">
<th scope="row">Google Base Feed</th>
<td>The URL for the feed is: <a href="<?php bloginfo('atom_url'); ?>?shopcart=true"><?php bloginfo('atom_url'); ?>?shopcart=true</a></td>
<?php /* FIXME: This will only work if permalinks is on */ ?>
</tr>

</table>

<h3>Shipping Options</h3>
<table class="form-table">
<tr valign="top">
<th scope="row">Shipping Calculation Method</th>
<?php $ssm = get_option('shopcart_shipping_method'); ?>
<td>
    <select name="shopcart_shipping_method" onchange="displayShippingTable(this);">
        <option value="free" <?php if ($ssm == 'free' || $ssm == '') echo 'selected="selected"'; ?>>Free Shipping</option>
        <option value="flat" <?php if ($ssm == 'flat') echo 'selected="selected"'; ?>>Flat Rate, By Country</option>
        <option value="peritem" <?php if ($ssm == 'peritem') echo 'selected="selected"'; ?>>Per Item, US Only</option>
        <option value="byweight" <?php if ($ssm == 'byweight') echo 'selected="selected"'; ?>>By Weight, US Only</option>
    </select>
</td>
</tr>
</table>

<span id="free-rate-table" <?php if ($ssm != 'free' && $ssm != '') echo 'style="display:none;"'; ?> class="ratetable"> 
    You are providing free shipping
</span>

<table id="flat-rate-table" <?php if ($ssm != 'flat') echo 'style="display:none;"'; ?> class="ratetable" >
    <tr valign="top">
        <th scope="column">Region</th>
        <th scope="column">Shipping Rate</th>
        <th></th>
    </tr>
    <tr class="templaterow">
        <td>
        <?php display_select_countries('shipping_flatrate_country[]', 'none'); ?>
        </td>
        <td>
        <input type="text" name="shipping_flatrate_rate[]" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span>
        </td>
        <td>Delete</td>
    </tr>

    <?php   $flatrate_table = get_option('shopcart_flatrate_table');
            if (is_array($flatrate_table)) :
                foreach ($flatrate_table as $row_key => $row_data) : 
                    if ($row_key != 'all_others') :?>
    <tr>
        <td>
        <?php display_select_countries('shipping_flatrate_country[]', $row_key); ?>
        </td>
        <td>
        <input type="text" name="shipping_flatrate_rate[]" value="<?php echo $row_data; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span>
        </td>
        <td>Delete</td>
    </tr>
    <?php           endif;
                endforeach;
                if (array_key_exists('all_others', $flatrate_table)) $other_rate = $flatrate_table['all_others'];
                else $other_rate = '';
            endif; ?>
    <tr class="insertrow">
        <td colspan="3" onclick="insertNewRow('flat-rate-table');">Insert new row</td>
    </tr>
    <tr>
        <td>
        Everywhere else<input type="hidden" name="shipping_flatrate_country[]" value="all_others"/>
        </td>
        <td>
        <input type="text" name="shipping_flatrate_rate[]" value="<?php echo $other_rate; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span>
        </td>
        <td></td>
    </tr>
</table>

<table id="peritem-rate-table" <?php if ($ssm != 'peritem') echo 'style="display:none;"'; ?> class="ratetable">
    <tr valign="top">
        <th scope="column">Base Rate</th>
        <th scope="column">Per Item Rate</th>
    </tr>

<?php
    $s_pir_base = get_option('shopcart_byitemshipping_baserate');
    $s_pir_item = get_option('shopcart_byitemshipping_itemrate');
?>

    <tr>
        <td>
        <input type="text" name="shopcart_byitemshipping_baserate" value="<?php echo $s_pir_base; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span>
        </td>
        <td>
        Plus <input type="text" name="shopcart_byitemshipping_itemrate" value="<?php echo $s_pir_item; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span> per item
        </td>
    </tr>
</table>

<table id="byweight-rate-table" <?php if ($ssm != 'byweight') echo 'style="display:none;"'; ?> class="ratetable">
    <tr valign="top">
        <th scope="column">Base Rate</th>
        <th scope="column">Rate Per Unit (<span class="weight_code"><?php echo $s_weight_unit; ?></span>)</th>
    </tr>

<?php
    $s_pir_base = get_option('shopcart_byweightshipping_baserate');
    $s_pir_item = get_option('shopcart_byweightshipping_unitrate');
?>

    <tr>
        <td>
        <input type="text" name="shopcart_byweightshipping_baserate" value="<?php echo $s_pir_base; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span>
        </td>
        <td>
        Plus <input type="text" name="shopcart_byweightshipping_unitrate" value="<?php echo $s_pir_item; ?>" size="4"/><span class="currency_code"><?php echo $s_currency; ?></span> per unit (<span class="weight_code"><?php echo $s_weight_unit; ?></span>)
        </td>
    </tr>
</table>


<h3>Tax Options</h3>
<p>Please set up <a href="https://checkout.google.com/sell/settings?section=TaxSetup">Tax Rules in Google Checkout Settings</a>.</p>

<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="shopcart_mid,shopcart_aid,shopcart_hide_empty_cart,shopcart_currency,shopcart_sandbox_mode,shopcart_weight_units,shopcart_byweightshipping_baserate,shopcart_byweightshipping_unitrate,shopcart_byitemshipping_baserate,shopcart_byitemshipping_itemrate,shopcart_shipping_method,shopcart_sandbox_mid,shopcart_first_color,shopcart_second_color,shopcart_third_color,shopcart_fourth_color" />

<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div>
